package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Staff;
import com.itheima.edu.info.manager.service.StaffService;

import java.util.Scanner;

public abstract class BaseStaffController {
    Scanner scanner = new Scanner(System.in);
    StaffService staffService = new StaffService();
    public void start() {
        staff: while (true){
            System.out.println("--------欢迎来到 <员工> 管理系统--------");
            System.out.print("请输入您的选择: 1.添加员工  2.删除员工  3.修改员工  4.查看员工  5.退出     ");
            String choice = scanner.next();
            switch (choice){
                case "1":
//                    System.out.println("添加员工");
                    addStaff();
                    break;
                case "2":
//                    System.out.println("删除员工");
                    deleteStaff();
                    break;
                case "3":
//                    System.out.println("修改员工");
                    updateStaff();
                    break;
                case "4":
//                    System.out.println("查询员工");
                    findAllStaff();
                    break;
                case "5":
                    System.out.println("退出系统");
                    System.out.println("欢迎下次使用");
                    break staff;
                default:
                    System.out.println("输入有误");
                    System.out.println("请重新输入");
                    break;
            }
        }
    }

    private void updateStaff() {
        boolean staffExist = staffService.staffExist();
        if (!staffExist){
            displayStaffIsNull();
        }else {
            System.out.print("请输入需删除ID");
            while (true){
                String sId = scanner.next();
                //判断id是否已经存在
                boolean sIdExist = staffService.sIdExist(sId);
                if (!sIdExist){
                    System.out.println("输入ID不存在");
                    System.out.println("请重新输入");
                }else {
                    Staff updateStaff = getStaff(sId);
                    staffService.updateStaff(updateStaff);
                    break;
                }
            }
        }
    }

    private void deleteStaff() {
        //判断数组中是否存在数据
        boolean staffExist = staffService.staffExist();
        if (!staffExist){
            displayStaffIsNull();
        }else {
            System.out.print("请输入需删除ID");
            while (true){
                String sId = scanner.next();
                //判断id是否已经存在
                boolean sIdExist = staffService.sIdExist(sId);
                if (!sIdExist){
                    System.out.println("输入ID不存在");
                    System.out.println("请重新输入");
                }else {
                    staffService.deleteStaff(sId);
                    System.out.println("删除成功");
                    break;
                }
            }
        }
    }

    private void findAllStaff() {
        boolean staffExist = staffService.staffExist();
        if (staffExist){
            Staff[] staffs = staffService.findAllStaff();
            for (int i = 0; i < staffs.length; i++) {
                if(staffs[i] != null){
                    System.out.println(staffs[i].toString());
                }
            }
        }else {
            displayStaffIsNull();
        }
    }

    private void displayStaffIsNull() {
        System.out.println("暂无数据");
        System.out.println("无法执行该操作");
        System.out.println("欢迎添加");
    }

    private void addStaff() {
        //判断数组是否满
        String sId;
        boolean full = staffService.isFull();
        if (full){
            System.out.println("空间已满");
            System.out.println("添加失败");
        }else {
            System.out.print("请输入员工id信息");
            while (true){
                sId = scanner.next();
                //判断该ID数组中是否有重复
                boolean exist = staffService.isExist(sId);
                if (exist) {
                    System.out.println("Id已存在");
                    System.out.print("请重新输入");
                }else {
                    break;
                }
            }
            Staff staff = getStaff(sId);
            boolean success = staffService.addStaff(staff);
            if (success){
                System.out.println("添加员工成功");
            }else{
                System.out.println("添加员工失败");
            }
        }
    }

    public abstract Staff getStaff(String sId);
}
